<?php
declare(strict_types=1);
namespace app\admin\service\article;

use app\common\model\Article;
use app\common\model\ArticleCate;

/**
 * 文章 文章分组 服务类
*/

class ArticleService {
    /**
     * 添加/编辑 用户
     */
    public static function saveOrEditUser($params,$admin_id){
        //id 大于0 时为编辑
        if (isset($params['id']) && $params['id'] > 0) {
            //编辑
            $params['state'] = $params['state'] ? 1 : 0;
            $model = Article::field('id,cate_id,state,title,description,thumb,text,sort')->with('ArticleCate')->find($params['id']);
            if ($model) {
                $model->save($params);
                return suc('成功',$model);
            }
            return suc('非法操作','',1);
        }
        //添加
        unset($params['id']);

        $model = new Article();
        $params['state'] = 1;
        $params['admin_id'] =$admin_id;
        $model->save($params);

        $model->ArticleCate = ArticleCate::field('id,name')->find($model->cate_id);
        return suc('成功',$model);
    }

    /**
     * 添加/编辑 用户分组
    */
    public static function saveOrEditUserCate($params){
        //id 大于0 时为编辑
        if (isset($params['id']) && $params['id'] > 0) {
            //编辑
            $params['state'] = $params['state'] ? 1 : 0;
            $model = ArticleCate::field('id,name,description,state')->find($params['id']);
            if ($model) {
                $model->save($params);
                return suc('成功',$model);
            }
            return suc('非法操作','',1);
        }
        //添加
        unset($params['id']);
        $model = new ArticleCate();
        $params['state'] = 1;
        $model->save($params);
        return suc('成功',$model);
    }
}